function Game(ele){//element
    //0.保存地图对象
    this.ele=document.querySelector(ele)

    //1.搞地图
    this.map=new Map(this.ele)//将地图标签对象传递给map构造函数，里面设置宽高等
    this.map.create()

    //2.搞食物
    this.food=new Food(this.ele)//将地图传给视图构造函数，后期追加食物
    this.food.create()

    //3.搞蛇
    this.snake=new Snake(this.ele)//
    this.snake.create()
    this.t=null
}

Game.prototype.start=function(){
    this.t=setInterval(()=>{
        //蛇动
        this.snake.move()

        //判断吃食
        let snakeX = this.snake.snakeObj[0].x * 20
        let snakeY = this.snake.snakeObj[0].y * 20
        let foodX = this.food.foodX
        let foodY = this.food.foodY

        if (snakeX == foodX && snakeY == foodY) {

            let last=this.snake.snakeObj[this.snake.snakeObj.length-1]

            this.snake.snakeObj.push({
                x:last.x,
                y:last.y,
                color:last.color,
                ele:null
            })
            this.snake.create()//重新绘制蛇
            //食物动
            this.food.move()
          
        }
        this.gameover()
    },150)
    //监控键盘上下左右键  来动态更改蛇的direction
    document.addEventListener('keydown',(e)=>{
        e=e||window.event
        //获取键盘
        switch(e.keyCode){
            case 38:
                if (this.snake.direction == 'bottom') {
                    this.end()
                }
                this.snake.direction='top'
                break;
            case 40:
                if (this.snake.direction == 'top') {
                    this.end()
                }
                this.snake.direction='bottom'
                break;
            case 37:
                if (this.snake.direction == 'right') {
                    this.end()
                }
                this.snake.direction='left'
                break;
            case 39:
                if (this.snake.direction == 'left') {
                    this.end()
                }
                this.snake.direction='right'
                break;
            default:
                break;
        }
    })
}

Game.prototype.stop=function(){
    clearInterval(this.t)
}

Game.prototype.gameover=function(){
    let sL=this.snake.snakeObj[0].x*this.snake.len
    let sT=this.snake.snakeObj[0].y*this.snake.len
    let maxL=this.ele.clientWidth
    let maxT=this.ele.clientHeight

    if(sL<0)this.end()
    if(sT<0)this.end()
    if(sL>maxL)this.end()
    if(sT>maxT)this.end()
}
Game.prototype.end=function(){
    clearInterval(this.t)
    this.t=null
    alert('GANE OVER!!!')
}